package cc.chengheng.security.config;

import cc.chengheng.security.filter.TokenAuthenticationFilter;
import cc.chengheng.security.filter.TokenLoginFilter;
import cc.chengheng.security.security.DefaultPasswordEncoder;
import cc.chengheng.security.security.TokenLogoutHandler;
import cc.chengheng.security.security.TokenManager;
import cc.chengheng.security.security.UnauthorizedEntryPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class TokenWebSecurityConfig extends WebSecurityConfigurerAdapter {

    private TokenManager tokenManager;
    private RedisTemplate redisTemplate;
    private DefaultPasswordEncoder defaultPasswordEncoder;
    private UserDetailsService userDetailsService;

    @Autowired
    public TokenWebSecurityConfig(TokenManager tokenManager,
                                  RedisTemplate redisTemplate,
                                  DefaultPasswordEncoder defaultPasswordEncoder,
                                  UserDetailsService userDetailsService) {
        this.tokenManager = tokenManager;
        this.redisTemplate = redisTemplate;
        this.defaultPasswordEncoder = defaultPasswordEncoder;
        this.userDetailsService = userDetailsService;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.csrf().disable();

        http.exceptionHandling()
                .authenticationEntryPoint(new UnauthorizedEntryPoint()) // 没有权限访问执行

                .and()
                .authorizeRequests().anyRequest().authenticated()

                .and()
                .logout().logoutUrl("/admin/acl/index/logout")// 退出路径
                .addLogoutHandler(new TokenLogoutHandler(tokenManager, redisTemplate)) // 退出执行哪个处理者

                // 自定义自己的过滤器
                .and()
                .addFilter(new TokenLoginFilter(tokenManager, redisTemplate,authenticationManager())) // 登录过滤器
                .addFilter(new TokenAuthenticationFilter(authenticationManager(), tokenManager, redisTemplate)); // 认证过滤器
    }

    // 认证管理构建
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(defaultPasswordEncoder);
    }


    /**
     * 不进行认证路径可以直接访问
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/api/**"); // 不认证授权
    }
}
